package com.hty.PowerChat;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.File;

import pclientbackend.ChatClient;
import pclientbackend.Pclientbackend;

public class ModifyPasswordActivity extends AppCompatActivity {

    ImageButton imageButton_back;
    Button button_confirm;
    EditText editText_pwOld, editText_pwNew, editText_pwNew1;
    ChatClient chatClient;
    String uname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); //使用自定义布局
        actionBar.setCustomView(R.layout.actionbar_modify_password);

        Intent intent = getIntent();
        uname = intent.getStringExtra("uname");

        imageButton_back = findViewById(R.id.imageButton_back);
        button_confirm = findViewById(R.id.button_confirm);
        imageButton_back.setOnClickListener(new ClickListener());
        button_confirm.setOnClickListener(new ClickListener());
        editText_pwOld = findViewById(R.id.editText_pwOld);
        editText_pwNew = findViewById(R.id.editText_pwNew);
        editText_pwNew1 = findViewById(R.id.editText_pwNew1);

        String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "PowerChat";
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String json = "{\"Host\":\"148.70.58.15:7889\"}";
        chatClient = Pclientbackend.getChatClient(path, json);
    }

    class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.imageButton_back:
                    finish();
                    break;
                case R.id.button_confirm:
                    String pwOld = editText_pwOld.getText().toString();
                    String pwNew = editText_pwNew.getText().toString();
                    String pwNew1 = editText_pwNew1.getText().toString();
                    if(!pwNew.equals("")) {
                        if (pwNew1.equals(pwNew)) {
                            if(!pwNew.equals(pwOld)){
                                chatClient.newPasswd(uname, pwOld, pwNew);
                                long r = chatClient.checkPwd(uname, pwNew);
                                if (r == 1) {
                                    Toast.makeText(ModifyPasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                                } else if(r == 2){
                                    Toast.makeText(ModifyPasswordActivity.this, "密码修改失败！", Toast.LENGTH_SHORT).show();
                                } else if(r == 3){
                                    Toast.makeText(ModifyPasswordActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                editText_pwNew.setError("新密码不能和原密码一样");
                            }
                        } else {
                            editText_pwNew1.setError("两次输入的密码不一样");
                        }
                    } else {
                        editText_pwNew.setError("密码不能为空");
                    }
                    break;
            }
        }
    }
}
